<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* 
            请求接口：http://120.78.190.136:8080/spboot/userLoginPostRequest

            为什么通过浏览器访问会报错呢?
                是因为该接口只能接受post请求，通过浏览器访问属于GET请求

                
            错误信息：
            from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
            需要在请求的时候，加上请求头信息
        */
        const request = new XMLHttpRequest();

        // 2.创建一个与服务器的链接
        request.open("POST", "http://www.biwuit.cn/spboot/userLoginPostRequest");
        // 如果是POST请求，需要加上该请求头信息
        request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        //是一个无符号长整型数，代表着一个请求在被自动终止前所消耗的毫秒数。
        request.timeout = 1000;
        // 3. 调用send()方法发送请求
        request.send("username=admin&password=66666");
        // 4.通过onreadystatechange绑定一个回调函数
        request.onreadystatechange = function () {
            if (request.readyState == 4 && request.status == 200) {
                console.log("post异步请求完成");
                console.log(request.responseText);
            }
        }
    </script>
</body>

</html>